﻿--简体中文
local version="0.1.0"
local names=JMacroLocale.names or {}
local L=JMacroLocale
JMacroHandlers={}

JMacro={
	debug=true,
	nextTryTime=0,
	interval = 0.2, -- 更新宏的周期（秒）
	player={
	},
	target={},
	lastTarget={},
}

--创建或编辑 宏
function JMacroEdit(jid,text,icon)
	local name = names[jid] or jid
	icon=icon or 1
	local index = GetMacroIndexByName(name)
	local _,_ body, _ = GetMacroInfo(macroIndex)
	if(body==text)then return end
	if(index and index~=0)then
		--JMacroDebug("edit macro name="..name.." text="..text.." icon="..icon);
		EditMacro(index, name, icon, text)
	else	
		--JMacroDebug("creating macro name="..name.." text="..text.." icon="..icon);
		index = CreateMacro(name, icon, text, 0)
	end
	return true
end

--pet 切换宠物技能的自动施法
function JMacroUpdatePetToggle()
	local text;
	if(hunter)then
		text=""
	elseif(warlock)then
		text=""
	end
end


------------------------------------------------------------------------------------------------------
-- Function for creating a new button
------------------------------------------------------------------------------------------------------
local function JMacroCreateButton(name, parent)
	-- Create the button frame
	local btn = CreateFrame("Button", name, parent, "SecureActionButtonTemplate");
	
	-- Set height
	btn:SetHeight(35);

	-- Set width
	btn:SetWidth(35);

	-- Enable it for clicks
	btn:SetScript("PreClick", ButtonClick);
	btn:RegisterForClicks("AnyUp");

	-- Position it
	btn:SetPoint("TOPLEFT", parent, "TOPLEFT", 0, 0);

	-- Set textures
	--btn:SetNormalTexture(profbars_frames[name].texturepath);
	--btn:SetHighlightTexture(profbars_frames[name].texturepath, 1.5);

	-- Set mouseover/mouseout
	btn:SetScript("OnEnter", OnEnter);
	btn:SetScript("OnLeave", OnLeave);
end


JMacroFrame=CreateFrame("Frame",nil,UIParent)

function JMacroUpdateItemMacros()
	JMacroFoodHandler("食")
	JMacroBandageHandler("绷")
	JMacroHPHandler("HP")
	JMacroMPHandler("MP")
end

local just_leave_battle=false
function JMacroFrame:OnEvent()
	--JMacroDebug(event,arg1,arg2)
	if(event=="ADDON_LOADED" and arg1=='JOneKeyBattle')then
		self:Init();
	elseif(event=="BAG_UPDATE")then
		JMacroScanBag();
		if(not InCombatLockdown())then
			JMacroUpdateItemMacros()
		end
	elseif(event=="SPELLS_CHANGED")then
		JMacroScanSpellbook();
	elseif(event=="PLAYER_LEAVE_COMBAT")then
		--JMacroUpdateItemMacros()
		JMacro.nextTryTime=GetTime()+0.02
	end
end

function JMacroFrame:OnUpdate()
	local t = GetTime()
	if(t > JMacro.nextTryTime)then
		JMacro.nextTryTime = t + JMacro.interval;
		
		if(InCombatLockdown())then
			JMacroUpdatePetToggle();
		else
			JMacro.player.level = UnitLevel("player")
			for k,foos in pairs(JMacroHandlers) do
				for i=1,#foos do
					local foo = foos[i]
					if(foo(k))then break end
				end
			end
		end

	end
end

JMacroFrame:RegisterEvent("ADDON_LOADED")

JMacroFrame:SetScript("OnEvent",JMacroFrame.OnEvent);
JMacroFrame:SetScript("OnUpdate",JMacroFrame.OnUpdate);
function JMacroFrame:Init()
	JMacroPrint(JMacroGetText("load"));
	JMacroFrame:RegisterEvent("BAG_OPEN")
	JMacroFrame:RegisterEvent("BAG_UPDATE")
	JMacroFrame:RegisterEvent("SPELLS_CHANGED")
	JMacroFrame:RegisterEvent("BANKFRAME_OPENED")
	JMacroFrame:RegisterEvent("BANKFRAME_CLOSED")	
	JMacroFrame:RegisterEvent("PLAYER_LEAVE_COMBAT")
	JMacroFrame:UnregisterEvent("ADDON_LOADED")	
	
	_,JMacro.player.class=UnitClass("player")
	
	SlashCmdList["JMACRO"] = JMacroFrame_Slash
	--商业技能
	SlashCmdList["JMACRORECIPE"] = JMacroRecipe_Slash
	SLASH_JMACRO1 = "/jmacro"
	SLASH_JMACRO2 = "/jm"
	SLASH_JMACRORECIPE1="/jr"
	JMacroScanBag()
	JMacroScanSpellbook()
	JMacroUpdateItemMacros()
	--[[
	-------------
	local prefix = "JMacroBar"
	local id = 1
	
	-- create bars
	local bar = CreateFrame("Frame", prefix..id, UIParent)

	bar:SetFrameStrata("MEDIUM")
	bar:SetClampedToScreen(true)
	bar:SetWidth(36); bar:SetHeight(36);
	bar:SetAttribute("actionpage", page)

	bar.GetButton = self.GetButton

	
	bar:SetID(id)
	--rawset(bar, "UpdateButtons", zExBars.UpdateExBarButtons)

	local button
	for i = 1, 12 do
		button =bar:GetButton(i)
		button:SetID(i)
		button:SetParent(bar)
	end
	bar:GetButton(1):ClearAllPoints()
	bar:GetButton(1):SetPoint("CENTER")

	if not zBar2Saves[prefix..id] then
		zBar2Saves[prefix..id] = zBar2:GetDefault(bar,"saves")
		zBar2Saves[prefix..id].pos = zBar2:GetDefault(bar,"pos")
	end
	
	
	-- create buttons
	local button
	for id = 1, 12 do
		button = CreateFrame("CheckButton", "JMButton"..id,UIParent,"SecureFrameTemplate,ActionBarButtonTemplate")
		_G["JMButton"..id.."NormalTexture"]:SetWidth(60)
		_G["JMButton"..id.."NormalTexture"]:SetHeight(60)
		_G["JMButton"..id.."NormalTexture"]:SetVertexColor(1.0, 1.0, 1.0, 0.5)
		button.buttonType = "ZEXBUTTON"
		button:SetParent(bar)
		button:Show()
	end

	--------------
	
	jm_movebutton = CreateFrame("Button", "jm_inscmove", JMacroFrame);

	-- Set height
	JMacroFrame:SetHeight(35);
	-- Set movable
	JMacroFrame:SetMovable(true);	
	-- Parent it to UIParent and position where we saved it
	JMacroFrame:SetPoint("TOPLEFT", UIParent, 300, 300);

	-- Set default width
	JMacroFrame:SetWidth(297.5);

	jm_movebutton:SetHeight(17.5);
	jm_movebutton:SetWidth(17.5);
	jm_movebutton:SetScript("OnMouseDown", jm_startdragging_insc);
	jm_movebutton:SetScript("OnMouseUp", jm_stopdragging_insc);
	jm_movebutton:SetPoint("TOPRIGHT", JMacroFrame, "TOPRIGHT");
	jm_movebutton:SetHighlightTexture("Interface\\Icons\\Spell_Arcane_PortalStormWind", 0.8);
	
	for i=1, 9 do
		if i~=3 then
			JMacroCreateButton("jm_inscbutton"..i, JMacroFrame);
		end
	end
	JMacroFrame:Show()
	jm_movebutton:Show()
		
			local btn;
			if not _G["profbars_enchbutton"..counter] then
				btn = CreateFrame("Button", "profbars_enchbutton"..counter, profbars_enchanting, "SecureActionButtonTemplate");
			else
				btn = _G["profbars_enchbutton"..counter];
			end
			btn:SetHeight(35);
			btn:SetWidth(35);
			btn:SetScript("PreClick", ButtonClick);
			btn:RegisterForClicks("AnyUp");
			local offsety = math.floor(i/9)*-35;
			local offsetx = ((i-math.floor(i/9)*9)-1)*35;
			btn:SetPoint("TOPLEFT", profbars_enchanting, "TOPLEFT", offsetx, offsety);
			btn:SetNormalTexture(greens[i]["texture"]);
			btn:SetHighlightTexture(greens[i]["texture"], 1.5);
			btn:SetScript("OnEnter", OnEnter);
			btn:SetScript("OnLeave", OnLeave);
			btn:Show();
			profbars_temp["enchantingindex"][counter] = {};
			profbars_temp["enchantingindex"][counter]["link"] = greens[i]["link"];
			profbars_temp["enchantingindex"][counter]["bag"] = greens[i]["bag"];
			profbars_temp["enchantingindex"][counter]["slot"] = greens[i]["slot"];
			counter = counter + 1;
]]--

end

------------------------------------------------------------------------------------------------------
-- What happens when we start dragging the Inscription frame
------------------------------------------------------------------------------------------------------
function jm_startdragging_insc()
	jm_inscription:StartMoving()
end

------------------------------------------------------------------------------------------------------
-- What happens when we stop dragging the Inscription frame
------------------------------------------------------------------------------------------------------
function jm_stopdragging_insc()
	db.offsets.insc.x = jm_inscription:GetLeft();
	db.offsets.insc.y = jm_inscription:GetBottom();
	jm_inscription:StopMovingOrSizing();
end

function JMacroFrame_Slash(msg)
	local a,b,c=strfind(msg, "(%S+)");
	if(a~=nil)then
	end
	if(c=="help")then
		print(JMacroGetText("help"))
		return;
	end
end

function JMacroDebug(...)
	if(JMacro.debug)then
		JMacroPrint(string.join(", ", tostringall(...)))
	end
end
function JMacroPrint(msg,r,g,b)
	if (r == nil or g == nil or b == nil) then
		r = 0.36;
		g = 0.74;
		b = 0.74;
	end
	DEFAULT_CHAT_FRAME:AddMessage(msg,r,g,b)
end

function JMacroGetText(str)
	return string.gsub(JMacroLocale[str] or "JMacro."..str,"%v",version)
end
function JMacroRecipe_Slash(msg)
	local a,b,c=strfind(msg, "(%S+)");
	if(a~=nil)then
	end
	if(c~=nil)then
		for i=1 ,GetNumTradeSkills() do
			local skillName, skillType, numAvailable, isExpanded, serviceType = GetTradeSkillInfo(i)
			if c==skillName then
				--DoTradeSkill(index [, repeat])
				DoTradeSkill(i)
			end
		end
	end
	
end